<?php

namespace App\Http\Controllers\Admin;
use \Hash;
use \DB;
use Illuminate\Http\Request;
use App\Http\Requests\RotateInsertRequest;
use App\Http\Requests\RotateUpdateRequest;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class RotateController extends Controller
{

	//轮播图显示
	public function getIndex(Request $request)
	{
		
		//链接数据库进行查找
		$rotates = DB::table('shop_rotate')->get();
		// dd($rotates);
		return view('admin.rotate.index',['rotates'=>$rotates]);
		
	}

	//轮播图添加
	public function getAdd(Request $request)
	{
		return view('admin.rotate.add');
	}

	//处理添加的数据
	 public function postDoadd(Request $request)
	 {
	 	$data = $request->only(['rname']);

	 	//进行图片处理
	 	if($request->hasFile('rpic'))
	 	{
	 		$ex = $request->file('rpic')->getClientOriginalExtension();
	 		$newname = md5(time().rand(1,9999));
	 		$request->file('rpic')->move('./upload/rotate/',$newname.'.'.$ex);
	 		$data['rpic']= '/upload/rotate/'.$newname.'.'.$ex;
	 	}else{
	 		return back()->with('error','未上传图片');
	 	}

	 	//链接数据库执行插入操作
	 	$res = DB::table('shop_rotate')->insert($data);
	 	if($res)
	 	{
	 		return redirect('/admin/rotate/index')->with('success','上传图片成功');
	 	}else{
	 		return back()->with('error','上传图片失败');
	 	}

	 }

	//轮播图的删除操作
	 public function postDelete(Request $request)
	 {
	 	//获取id
	 	$id = $request->input('uid');
	 	//执行删除操作
	 	$res = DB::table('shop_rotate')->where('rid',$id)->delete();

	 	echo $res;

	 }
	 //轮播图的效果展示
	 public function getShow(Request $request)
	 {
	 	
	 	//获取所有的图片
	 	$rotates = DB::table('shop_rotate')->get();
	 	// dd($rotates);
	 	return view('admin.rotate.show',['rotates'=>$rotates]);

	 }

}
